
package com.htj.erp.service;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.kedacom.mybatis.ext.PageHelper;
import com.kedacom.security.service.GenericCpsService;
import com.htj.erp.dao.HCustomerDao;
import com.htj.erp.dao.mybatis.HCustomerMapper;
import com.htj.erp.exception.BusinessException;
import com.htj.erp.model.jpa.HCustomer;
import com.htj.erp.model.mybatis.HCustomerPO;
import com.htj.erp.vo.HCustomerVO;

import com.kedacom.utils.BeanUtils;
import com.kedacom.utils.BeanMapper;
import com.kedacom.utils.PageUtil;
import com.kedacom.utils.vpc.Converter;
import com.kedacom.utils.vpc.DefalutConverter;
/**
 * 
 *
 * 
 * HCustomerService
 * H_CUSTOMER service 处理类
 * 这个基本类一般继承com.kedacom.security.service.GenericCpsService
 * 如果自己实现转换类，通过重写getXXConveter方法。
 * chenghongtao
 * 2016-11-04 13:04:28
 * HTJ
 * @version 1.0.0
 *
 */
@Service
public class HCustomerService extends GenericCpsService<HCustomer,HCustomerPO,HCustomerVO,Long>  {

    
	@Resource
	private HCustomerDao hCustomerDao;
	
	@Resource
	private HCustomerMapper hCustomerMapper;
	
	/**
	 * 获取jpa的dao
	 */
	@Override
	protected  HCustomerDao getEntityJpaDao ()
	{
		return this.hCustomerDao;
	}
	 
	  
	/**
	 * 获取 mubatis 的dao
	 */
	@Override
	protected  HCustomerMapper getEntityDao ()
	{
		return this.hCustomerMapper;
	}
	
	
	
	/**
	 * 
	 * saveOrUpdate 保存或者更新
	 * @param hCustomerVO
	 * @return
	 *HCustomerVO
	 * @exception
	 * @since  1.0.0
	 */
	public HCustomerVO saveOrUpdate(HCustomerVO hCustomerVO) throws Exception
	{
	    if(hCustomerVO.getId() == null || hCustomerVO.getId() == 0L)
	    {
	    	if(this.getEntityJpaDao().findByCusName(hCustomerVO.getCusName()) != null)
	    	{
	    		throw new BusinessException(hCustomerVO.getCusName() + "已经存在");
	    	}
	        return this.add(hCustomerVO);
	    }
	    else
	    {
	    	HCustomer cus = this.getEntityJpaDao().findByCusName(hCustomerVO.getCusName());
	    	if(cus != null && !cus.getId().equals(hCustomerVO.getId()))
	    	{
	    		throw new BusinessException(hCustomerVO.getCusName() + "已经存在");
	    	}
	        return this.update(hCustomerVO);
	    }
	    
	}


	@Override
	public void delById(Long id) throws Exception {
		Integer num = this.getEntityDao().isCustomerUsed(id);
		if(num > 0)
			throw new BusinessException("客户已经被使用无法删除");
		super.delById(id);
	}


	@Override
	public void delByIds(Long[] ids) throws Exception {
		for(Long id: ids)
			delById(id);
	}
	
	
	
}
